// ======================================================================
// Copyright (C) 2013 Hell-Prototypes. / www.hellprototypes.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// ======================================================================
#ifndef USB_THREAD_H
#define USB_THREAD_H

#include <QMutex>
#include <QThread>
#include "usb.h"
#include "define.h"
#include <qwt_system_clock.h>

#define USB_BULK_READ_LEN   (16*1024)

class USB_Thread : public QThread
{
    Q_OBJECT
public:
    explicit USB_Thread(QObject *parent = 0);

    bool connect_usb();
    void disconnect_usb();
    bool set_sampling_divider(int divider);
    void stop_data_read();
    //char *get_buffer_pointer() { return m_rle_data_buffer; }

    void fifo_take(char *take_buffer, int len);

private :
    /*variable*/
    usb_dev_handle *m_usb_dev; /* the device handle */
    bool run_flag;
    //char m_rle_data_buffer[USB_BULK_READ_LEN];

    QMutex mutex;
    QVarLengthArray<char, USB_BULK_READ_LEN> m_fifo_array;

    //QwtSystemClock m_sys_clock;

    /*usb function*/
    bool usb_connect_ctrl();
    usb_dev_handle* open_dev(void);
    bool send_ctrl_cmd(char *cmd_data, int cmd_len);
    //int read_bulk_data(char *read_buffer, int len);

    bool RECFG_FPGA();
    bool RESET_FPGA();

protected:
    void run();

signals:
    void rle_data_ready(int len);
    void usb_error(int err_code);
    void note_message(QString msg);

public slots:
    
};

#endif // USB_THREAD_H
